import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

type Result = {
  success: boolean;
  Message: string;
  Data?: Array<any>;
};

type ResultImport = {
  success: boolean;
  ErrorList?: Array<any>;
  SuccessCount?: number;
};

type ResultTable = {
  success: boolean;
  Message: string;
  Data?: {
    /** 列表数据 */
    data: Array<any>;
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    pageIndex?: number;
  };
};

export const getPage = (module: string, data?: object) => {
  return http.request<ResultTable>("get", `${baseUrlApi}/${module}/page`, {
    params: data
  });
};

export const exportExcel = (module: string, data?: object) => {
  return http.request<File>(
    "post",
    `${baseUrlApi}/${module}/export`,
    {
      data
    },
    {
      responseType: "blob"
    }
  );
};

export const getList = (module: string, data?: object) => {
  return http.request<Result>("get", `${baseUrlApi}/${module}/list`, {
    params: data
  });
};

export const add = (module: string, data?: object) => {
  return http.request<Result>("post", `${baseUrlApi}/${module}/add`, {
    data
  });
};

export const update = (module: string, data?: object) => {
  return http.request<Result>("post", `${baseUrlApi}/${module}/update`, {
    data
  });
};

export const del = (module: string, data?: object) => {
  return http.request<Result>("post", `${baseUrlApi}/${module}/delete`, {
    data
  });
};

export const setStatus = (module: string, data?: object) => {
  return http.request<Result>("post", `${baseUrlApi}/${module}/SetStatus`, {
    data
  });
};

export const importData = (module: string, data?: object) => {
  return http.request<ResultImport>(
    "post",
    `${baseUrlApi}/${module}/Import`,
    { data },
    {
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
};
